//
//  Bundle.swift
//  ChinaPoem-Swift
//
//  Created by jh on 2022/11/29.
//

import UIKit

extension Bundle {
    /// 当前项目bundle
    static var bundle: Bundle {
        return Bundle.main //Bundle(for: TabBarController.self)
    }
    
    /// 获取bundle中文件路径
    static func path(resource: String, ofType: String) -> String? {
        return Bundle.bundle.path(forResource: resource, ofType: ofType)
    }
    
    /// Resours文件bundle
    static var resouseBundle: Bundle? {
        if let budl = Bundle.path(resource: "images", ofType: "bundle") {
            return Bundle(path: budl)
        } else {
            return nil
        }
    }
    
    // reference: https://stackoverflow.com/a/51241158
    public var icon: UIImage? {
        guard let icons = infoDictionary?["CFBundleIcons"] as? [String: Any] else { return nil }
        guard let primaryIcon = icons["CFBundlePrimaryIcon"] as? [String: Any] else { return nil }
        guard let iconFiles = primaryIcon["CFBundleIconFiles"] as? [String] else { return nil }
        guard let lastIcon = iconFiles.last else { return nil }
        return UIImage(named: lastIcon)
    }
}
